package com.yumei.trader.gym.ktx

import android.annotation.SuppressLint
import android.provider.Settings
import androidx.appcompat.app.AppCompatActivity
import com.yumei.trader.gym.net.NetWork
import com.yumei.trader.gym.`object`.BaseResponse
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import androidx.lifecycle.lifecycleScope
import com.appsflyer.AppsFlyerLib
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@SuppressLint("HardwareIds")
fun AppCompatActivity.getGuideUrl(mCallBack: MCallBack) {
    val appCompatActivity = this
    this.lifecycleScope.launch(Dispatchers.IO) {
        NetWork.service.getGuide(Settings.Secure.getString(
                appCompatActivity.contentResolver,
                Settings.Secure.ANDROID_ID
        ), AppsFlyerLib.getInstance().getAppsFlyerUID(appCompatActivity)).enqueue(object : Callback<BaseResponse<String>> {
            override fun onResponse(
                    call: Call<BaseResponse<String>>,
                    response: Response<BaseResponse<String>>
            ) {
                response.body()?.let {
                    if (it.isSuccess && !it.explained.isNullOrBlank()) {
                        appCompatActivity.lifecycleScope.launch(Dispatchers.Main) {
                            mCallBack.onSuc(it.explained!!)
                        }
                    }
                }
            }

            override fun onFailure(call: Call<BaseResponse<String>>, t: Throwable) {
                mCallBack.onFailed()
            }
        })

    }
}